﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace jnxcLogin
{
    public partial class SetUser : System.Web.UI.Page
    {
        Maticsoft.BLL.UserInfo um = new Maticsoft.BLL.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            string loginName = this.txt_name.Text;
            string loginPass = this.txt_pass.Text;
            string remark = this.txt_remark.Text;

            Maticsoft.Model.UserInfo u = new Maticsoft.Model.UserInfo();
            

            u.loginName = loginName;
            u.loginPass = loginPass;
            u.remark = remark;

            if (um.Add(u) > 0)
            {
                Maticsoft.Common.MessageBox.Show(this,"添加用户成功！");
                Bind();
            }

        }

        private void Bind()
        {
            DataSet ds = um.GetList("");

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            Bind();
            //if (Session["login"] != null)
            //{
            //    DataSet ds = Session["login"] as DataSet;

            //    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

            //    Bind(user_id);
            //}
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_bj")
            {
                if (e.CommandArgument!=null)
                {
                    Session["edit_user_id"] = e.CommandArgument;

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FrameEditUser.aspx','500px','260px');</script>");
                }
              
            }
            if (e.CommandName == "lbn_sq")
            {
                if (e.CommandArgument != null)
                {
                    Session["sendPId"] = e.CommandArgument;

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPopWin('SendPower.aspx','1000px','600px');</script>");
                }

            }
            if (e.CommandName == "lbn_del")
            {
                if (e.CommandArgument != null)
                {
                    int user_id = Convert.ToInt32(e.CommandArgument);

                    if (um.Delete(user_id))
                    {
                        Bind();
                    }
                }
            }
        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            Bind();
        }

        //删除选定项
        protected void btn_delAll_Click(object sender, EventArgs e)
        {
            List<string> li_check = GetCheckBoxForGridView(this.GridView1, "cbo_check", "lbl_user_id");//获取选定项

            foreach (string user_id in li_check)
            {
                um.Delete(int.Parse(user_id));
            }
            Bind();
        }

        public  List<string> GetCheckBoxForGridView(GridView gv, string boxName, string labelName)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < gv.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)gv.Rows[i].Cells[0].FindControl(boxName);
                if (cb.Checked)
                {
                    Label lb = (Label)gv.Rows[i].Cells[0].FindControl(labelName);
                    list.Add(lb.Text);
                }
            }

            return list;

        }
    }
}